﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Data;
using MockPrjTeam4.Biz;
using MockPrjTeam4.Entity;
using MockPrjTeam4.Common;

namespace MockPrjTeam4
{
    public partial class OrganisationList : System.Web.UI.UserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable org = new DataTable();
            //BindData();
            if (chkInactive.Checked)
            {
                org = OrganisationBiz.GetTableOrganisations(true);
            }
            else
            {
                org = OrganisationBiz.GetTableOrganisations(false);
            }
            gvListOrg.DataSource = org;
            gvListOrg.DataBind();
            if (org.Rows.Count == 0)
            {
                lblMessageResult.Text = "No more results";
            }
            else
            {
                lblMessageResult.Text = "";
            }
        }



        protected void btnCreate_Click(object sender, EventArgs e)
        {
            lblMessageResult.Text = "";
            Response.Redirect("../Forms/AddOrganisation.aspx");
        }

        protected void chkInactive_CheckedChanged(object sender, EventArgs e)
        {
            DataTable org = new DataTable();
            bool inActive;
            if (chkInactive.Checked) inActive = true;
            else inActive = false;
            string range = "";
            if (hdffilter.Value != "")
            {
                switch (hdffilter.Value)
                {
                    case "SearchAll":
                        {
                            org = OrganisationBiz.GetTableOrganisations(true);
                            break;
                        }
                    case "Search09":
                        {
                            org = OrganisationBiz.Search("[0-9]%", inActive);
                            break;
                        }
                    case "SearchAE":
                        {
                            Session["range"] = "[A-E]%";
                            range = Convert.ToString(Session["range"].ToString(), CultureInfo.CurrentCulture);
                            org = OrganisationBiz.Search(range, inActive);
                            break;
                        }
                    case "SearchFJ":
                        {
                            org = OrganisationBiz.Search("[F-J]%", inActive);
                            break;
                        }
                    case "SearchKN":
                        {
                            org = OrganisationBiz.Search("[K-N]%", inActive);
                            break;
                        }
                    case "SearchOR":
                        {
                            org = OrganisationBiz.Search("[O-R]%", inActive);
                            break;
                        }
                    case "SearchSV":
                        {
                            org = OrganisationBiz.Search("[S-V]%", inActive);
                            break;
                        }
                    case "SearchWZ":
                        {
                            org = OrganisationBiz.Search("[W-Z]%", inActive);
                            break;
                        }
                }
                if (org.Rows.Count == 0)
                {
                    lblMessageResult.Text = "No more results";
                }
                else
                {
                    lblMessageResult.Text = "";
                }
                gvListOrg.DataSource = org;
                gvListOrg.DataBind();
            }
            else
            {
                DataTable orgChild;
                if (chkInactive.Checked)
                {
                    orgChild = OrganisationBiz.GetTableOrganisations(true);
                }
                else
                {
                    orgChild = OrganisationBiz.GetTableOrganisations(false);
                }
                if (orgChild.Rows.Count == 0)
                {
                    lblMessageResult.Text = "No more results";
                }
                else
                {
                    lblMessageResult.Text = "";
                }
                gvListOrg.DataSource = orgChild;
                gvListOrg.DataBind();
            }
        }

        protected void gvListOrg_PageIndexedChanging(object sender, GridViewPageEventArgs e)
        {
            gvListOrg.PageIndex = e.NewPageIndex;
            gvListOrg.DataBind();
        }
        
        protected void Click(object sender, CommandEventArgs e)
        {
            hdffilter.Value = e.CommandName;
            DataTable org = new DataTable();
            bool inActive;
            if (chkInactive.Checked) inActive = true;
            else inActive = false;
            string range = "";
            switch (e.CommandName)
            {
                case "SearchAll":
                    {
                        org = OrganisationBiz.GetTableOrganisations(true);
                        break;
                    }
                case "Search09":
                    {
                        org = OrganisationBiz.Search("[0-9]%", inActive);
                        break;
                    }
                case "SearchAE":
                    {
                        Session["range"] = "[A-E]%";
                        range = Convert.ToString(Session["range"].ToString(), CultureInfo.CurrentCulture);
                        org = OrganisationBiz.Search(range, inActive);
                        break;
                    }
                case "SearchFJ":
                    {
                        org = OrganisationBiz.Search("[F-J]%", inActive);
                        break;
                    }
                case "SearchKN":
                    {
                        org = OrganisationBiz.Search("[K-N]%", inActive);
                        break;
                    }
                case "SearchOR":
                    {
                        org = OrganisationBiz.Search("[O-R]%", inActive);
                        break;
                    }
                case "SearchSV":
                    {
                        org = OrganisationBiz.Search("[S-V]%", inActive);
                        break;
                    }
                case "SearchWZ":
                    {
                        org = OrganisationBiz.Search("[W-Z]%", inActive);
                        break;
                    }
            }
            if (org.Rows.Count == 0)
            {
                lblMessageResult.Text = "No more results";
            }
            else
            {
                lblMessageResult.Text = "";
            }
            gvListOrg.DataSource = org;
            gvListOrg.DataBind();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int id = (int)gvListOrg.SelectedDataKey.Values["OrganisationId"];
            Response.Redirect("../Forms/EditOrganisation.aspx?ID=" + id.ToString());
        }

        protected void lkbtnOrgName_Click(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("False"))
            {
                int orgId = Methods.ConvertNumber(e.CommandName);
                OrganisationBiz.SetOrgActive(orgId);
            }
            Response.Redirect(string.Format("../Forms/EditOrganisation.aspx?ID={0}", e.CommandName));
        }
    }
}